﻿--[[
@file   Offline dialog control.
@author jameszhai
@date   2017-04-15
--]]

TOfflineCtrl = 
{
    InitCtrl = function()
        if TOfflineCtrl.bInited then
            return;
        end;

        ierd_tgp.tpf_ui.Require("offline\\view.lua");
        ierd_tgp.tpf_ui.Require("offline\\module.lua");
        ierd_tgp.tpf_ui.Require("offline\\game_list.lua");
        ierd_tgp.tpf_ui.Require("offline\\frame_bottom.lua");

        -- 初始化界面
        TOfflineView.InitView();
        TOfflineModule.InitModule();

        comm_center.reg_service("Svr_GetGameIcon", "TOfflineCtrl.OnGetGameIcon");

        -- 离线模式
        comm_center.listen_broadcast("Msg_OfflineLogin", "TOfflineCtrl.OnOfflineLogin");

        TOfflineCtrl.bInited = true;
    end;

    UnInitCtrl = function()
        TOfflineModule.UnInitModule();
        TOfflineView.UnInitView();
    end;

    -- 获取游戏图标
    OnGetGameIcon = function(zParam)
        local   zInfo = TOfflineModule.GetGameInfo(zParam["GameID"]);

        if nil ~= zInfo then
            zParam["icon"] = zInfo.banner_icon_url;
        else
            zParam["icon"] = "";
        end;

        return zParam;
    end;

    -- 通知列表更新
    NotifyListUpdate = function()
        TOfflineView.SetMyGameList(true);
    end;

    -- 更新游戏列表
    UpdateMyGameList = function(nEvtID, sJson)
        TOfflineModule.UpdateMyGameList();
        TOfflineCtrl.NotifyListUpdate();
    end;

    -- 更新游戏基本信息
    UpdateGameBaseInfo = function(nEvtID, sJson)
        TOfflineModule.UpdateGameInfo();
        TOfflineCtrl.NotifyListUpdate();
    end;

    -- 更新单个游戏的信息
    update_single_game_base_info = function(param_tbl)
        TOfflineModule.update_single_game_base_info(param_tbl);
    end;

    -- 批量更新我的游戏列表数据
    update_batch_game_base_info = function(param_tbl)
        TOfflineModule.update_batch_game_base_info(param_tbl);
    end;
    -- 更新游戏服务器
    UpdateGameServers = function(nEvtID, sJson)
    end;

    -- 更新游戏状态
    UpdateGameState = function(nEvtID, sJson)
        if nil ~= sJson then
            local   zParam = base_utility.json_to_table(sJson);

            if nil == zParam then
                TGPLog.LogE("OfflineC", "UpdateState, J=>T fail.");
                return;
            end;

            if not TOfflineModule.UpdateGameState(zParam.game_id) then
                TGPLog.LogE("OfflineC", "Update state fail.");
                return;
            end;

            if 0 == zParam.game_id then
                TOfflineView.SetGameState(TOfflineModule.GetAllState())
            else
                TOfflineView.UpdateState(TOfflineModule.GetGameState(zParam.game_id));
            end;
        else
            TGPLog.LogE("OfflineC", "Update game state, json is nil.");
        end;
    end;

    -- 更新插件状态
    UpdatePluginState = function(nEvtID, sJson)
    end;

    -- 显示离线模式对话框
    ShowOffline = function()
        TOfflineView.ShowView();
    end;

    -- 离线模式登录
    OnOfflineLogin = function(zParam)
        local pTPFUI = base_utility.get_component_adapter("ITpf_ui",
                            "ierd_tgp.tpf_ui.GetTPFUI();");

        if nil ~= pTPFUI then
            pTPFUI:AddLuaListener(ierd_tgp.game_library.UPDATE_VISIBLE_GAME_LIST, 
                                  "TOfflineCtrl.UpdateMyGameList");
            pTPFUI:AddLuaListener(ierd_tgp.game_library.UPDATE_ALL_GAME_BASE_INFO, 
                                  "TOfflineCtrl.UpdateGameBaseInfo");
            pTPFUI:AddLuaListener(ierd_tgp.game_library.UPDATE_GAME_SERVERS, 
                                  "TOfflineCtrl.UpdateGameServers");
            pTPFUI:AddLuaListener(ierd_tgp.game_library.UPDATE_GAME_STATE, 
                                  "TOfflineCtrl.UpdateGameState");
            pTPFUI:AddLuaListener(ierd_tgp.game_library.UPDATE_PLUGIN_STATE, 
                                  "TOfflineCtrl.UpdatePluginState");
            comm_center.listen_broadcast("Msg_UpdateGameBaseInfo",
                TOfflineCtrl.update_single_game_base_info);
            comm_center.listen_broadcast("Msg_BatchUpdateGameBaseInfo",
                TOfflineCtrl.update_batch_game_base_info);
        else
            TGPLog.LogE("OfflineC", "Offline login, get TPF UI fail.");
        end;

        login_model.running_mode = login_model.OFFLINE_MODE;

        -- 显示离线界面
        TOfflineView.ShowView();

        -- 个人信息
        TOfflineModule.SetSelfInfo(zParam);

        -- 显示个人信息
        TOfflineView.SetSelfInfo(zParam);
    end;

    BroadcastShowFinish = function()
        local     zParam = {};
        zParam.isfirst = TOfflineCtrl.first_show;
        TOfflineCtrl.first_show = 0;
        comm_center.send_broadcast("Msg_MainFrameShowFinish", zParam);
    end;

    bInited = false;
    first_show = 1;
};

TOfflineCtrl.InitCtrl();